package com.nf.health.app.healthbank;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.healthbank.fragment.PublicHealthFragment;
import com.nf.health.app.healthbank.fragment.TreatDiseaseFragment;
import com.nf.health.app.utils.DialogHelp;
import com.nf.health.app.utils.ScreentUtils;
import com.nf.health.app.utils.ViewUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 健康银行
 */
public class MyHealthyBankActivity extends BaseActivity {

    @Bind(R.id.titlebar_right_view)
    ImageView titlebarRightView;
    /**
     * 记录按下的页签索引
     */
    private int index;
    private int currentTabIndex;
    private Fragment[] fragments;
    private View mIndicate;
    private RadioGroup mRbHealthBank;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_health_bank);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initView() {
        ViewUtil.setText(this, R.id.titlebar_title_tv, "健康银行");
        titlebarRightView.setImageResource(R.drawable.inquiry_question);
        fragments = new Fragment[]{new TreatDiseaseFragment(), new PublicHealthFragment()};
        getSupportFragmentManager().beginTransaction().add(R.id.ii_middle1, fragments[0]).show(fragments[0]).commit();
        mRbHealthBank = ViewUtil.getView(this, R.id.rg_health_bank);
        mIndicate = ViewUtil.getView(this, R.id.view_indicate);
        changeIndicaterView(0);
    }

    private void initListener() {
        //切换fragment
        mRbHealthBank.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_treat_disease:
                        index = 0;
                        break;
                    case R.id.rb_public_health:
                        index = 1;
                        break;
                    default:
                        break;
                }
                if (currentTabIndex != index) {
                    changeIndicaterView(index);
                    FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
                    trx.hide(fragments[currentTabIndex]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.ii_middle1, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();
                }
                currentTabIndex = index;
            }
        });
        //右上角提示
        titlebarRightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogHelp.showH5Dialog(MyHealthyBankActivity.this, Constants.H5_TIP_HEALTH_BANK_INTRODUCE);
            }
        });
    }

    //控制蓝色横线的的位置
    private void changeIndicaterView(int index) {
        LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) mIndicate.getLayoutParams();
        p.width = ScreentUtils.getScreenWidth(this) / 2;
        if (0 == index) {
            p.leftMargin = 0;
        } else {
            p.leftMargin = ScreentUtils.getScreenWidth(this) / 2;
        }
        mIndicate.setLayoutParams(p);
    }
}
